package com.bitBusy.manageyourriches.activity;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.database.contactClasses.ExpenseDetails;
import com.bitBusy.manageyourriches.database.managers.DatabaseInteractor;
import com.bitBusy.manageyourriches.database.managers.DateTimeManager;
import com.bitBusy.manageyourriches.utilityClasses.adapters.ImageButtonAdapterGridView;
import com.bitBusy.manageyourriches.utilityClasses.checkers.NullChecker;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayInputTextUsers;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;
import com.bitBusy.manageyourriches.utilityClasses.navigation.ActionBarChoice;
import com.bitBusy.manageyourriches.utilityClasses.navigation.GoHome;
import com.bitBusy.manageyourriches.utilityClasses.pickers.DateChangeListener;
import com.bitBusy.manageyourriches.utilityClasses.pickers.TimeChangeListener;
import com.bitBusy.manageyourriches.utilityClasses.setters.AddAddNewToMap;

public class ConfirmActivity extends FragmentActivity  implements  DisplayInputTextUsers,
					DateChangeListener, TimeChangeListener
{
	
	/** Default value for amount entered */ 
	private static final double default_amount = 0;
	
	/**Padding for compound view*/
	public static final int DEFAULTPADDING = 20;
	/**left displ for note added toast*/
	private static final int TOASTXAXIS = 200;
	
	/**TOP displ for note added toast*/
	private static final int TOASTYAXIS = -300;


	/**FUTURE DATE WARNING TEXT*/
	private static final String FUTUREDATESELECTED = "Careful! You selected a date in the future - be sure to spend!";
	
	/**FUTURE TIME WARNING TEXT*/
	private static final String FUTURETIMESELECTED = "Careful! You selected a time in the future - be sure to spend!";

	/** String to be displayed when record saved*/
	private static final String RECORDSAVEDPROMPT = "Expense record added!";

	/** String to be displayed when note added*/
	private static final String NOTEADDEDPROMPT = "Note added!";
	
	/**Default string for the nullCheck method*/
	private static final String DEFAULTSTRING = "default";

		
	/**Default pic id for payment method*/
	public static final int PAYMENTMETHODPICID = R.drawable.paymentmethod;
		
	public static final String DEFAULTPAYMENTMETHOD = "cash";
	/**Category name*/
	private String myCategoryName;
	
	/**Category image*/
	private int myCategoryImg;
	
	/**Subcategory chosen*/
	private String mySubcategoryChosen;
	
	/**Payment method chosen*/
	private static String myPaymentMethodChosen;
	
	

	/**Previous payment method chosen view */
	private View myPreviousView;
	

	/**Flag indicating if a new payment info being set*/
	private boolean myNewPaymentMethod;

	/**Flag indicating if a new subcategory was entered*/
	private boolean myNewSubcategory;
	
	/**Flag indicating if new note is to be added*/
	private boolean myNewNote;
	
	/**Database interactor object for this activity*/
	private DatabaseInteractor myDBInteractor;
	
	/** Option to be added at end of subcategories and payment info maps*/
	private String ADDNEW = "add new";
	
	 private static final String DELETETITLE = "Delete subcategory";
	    
	 private static final String DELETEMESSAGE = "Delete subcategory: ";
	  
	
	/**Amount entered*/
	private double myAmount;

	/**Note entered*/
	private String myNote;

	
	
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_confirm);
		myDBInteractor = new DatabaseInteractor(this);
		receiveIntentAndProcess();
	}
	
	/** Method to process the intent object that invoked this activity
	 * Expected to be from HomeActivity
	 */

	private void receiveIntentAndProcess()
	{
		Intent intent = getIntent();
		myAmount = intent.getDoubleExtra(HomeActivity.AMOUNT_CHOSEN, default_amount);   	
		myCategoryName = intent.getStringExtra(HomeActivity.CATEGORY_CHOSEN);
		myCategoryImg = intent.getIntExtra(HomeActivity.CATEGORY_IMAGE_CHOSEN, (int)default_amount);
		if (myCategoryName != null)
		{
			setCurrentDateAndTime();
			setEnteredAmount();
			setChosenCategory();
			setPaymentMethods();
			setSubcategoryGridView();
		}
	}
	
	
	/** Method to set current date and time on view*/
	private void setCurrentDateAndTime()
	{
		Button dateButton = (Button) findViewById(R.id.confirm_dateButton);
		dateButton.setText(DateTimeManager.getCurrentDate());
		
		Button timeButton = (Button) findViewById(R.id.confirm_timeButton);
		timeButton.setText(DateTimeManager.getCurrentTime());
	
	}
	
	/** Method to set category chosen on view 
	 * @param the_categoryChosen
	 */
	private void setChosenCategory()
	{
		TextView categoryText = (TextView) findViewById(R.id.confirm_categoryName);
		ImageView categoryImage = (ImageView) findViewById(R.id.confirm_categoryIMG);
		if (!NullChecker.checkForNullValues(new Object[]{categoryText, myCategoryName, categoryImage, myCategoryImg}))
		{
			categoryText.setText(myCategoryName);
			categoryImage.setImageResource(myCategoryImg);
		}
	}
	
	/**Method to set grid view of subcategories*/
	private void setSubcategoryGridView()
	{

		if (myDBInteractor != null && myCategoryName != null)
		{
			//mySubcategoryChosen = myDBInteractor.getPrevChosenSubcategory(myCategoryName)==null?"":myDBInteractor.getPrevChosenSubcategory(myCategoryName);
			GridView gridview = (GridView) findViewById(R.id.confirm_gridview);
			final ImageButtonAdapterGridView imgButtonAdapterObj = 
		    		new ImageButtonAdapterGridView(this,AddAddNewToMap.getModifiedMap(myDBInteractor.getSubcategories(myCategoryName), HomeActivity.CATEGORYADDNEWIMG));
		    gridview.setAdapter(imgButtonAdapterObj);
		     gridview.setOnItemClickListener(new OnItemClickListener() {
		        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	Map<String, Integer> subcategoryChosen = imgButtonAdapterObj.getItem(position);
	        	if (subcategoryChosen != null)
	        	{
	        		String subcategory = subcategoryChosen.keySet().toArray(new String[subcategoryChosen.keySet().size()])[0];
	        		if (subcategory != null)
	        		{
	        			if(ADDNEW.equals(subcategory))       		
			        	{
			        		addNewSubcategory();
			        	}
			        	else
			        	{
			        		mySubcategoryChosen = subcategory;
			        		saveEntry(findViewById(R.id.confirmView));
			        	}
			        }
		     }
		   }
		});
		     gridview.setOnItemLongClickListener(new OnItemLongClickListener(){
					public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id){
						Map<String, Integer> categoryChosenMap = imgButtonAdapterObj.getItem(position);
			        	if (categoryChosenMap != null)
			        	{
			        		final String subcategory = categoryChosenMap.keySet().toArray(new String[categoryChosenMap.keySet().size()])[0];
			        		if (!AddAddNewToMap.ADDNEW.equals(subcategory))
			        		{
			        			DisplayMessages.displayStandardDialog(ConfirmActivity.this,
										DELETETITLE, DELETEMESSAGE.concat(subcategory).concat(DialogBoxPrompts.QUESTIONMARK.getPrompt()), 
										DialogBoxPrompts.DIALOGOKPROMPT.getPrompt(), 
										new DialogInterface.OnClickListener() {
					    					public void onClick(DialogInterface dialog,int id) {
					    					if (myDBInteractor.deleteSubcategory(myCategoryName, subcategory))
					    					{
					    						setSubcategoryGridView();
					    					}
					    					}}
										, DialogBoxPrompts.DIALOGCANCELPROMPT.getPrompt(), 
										new DialogInterface.OnClickListener() {
			    					public void onClick(DialogInterface dialog,int id) {
			    						dialog.cancel();
			    					}
			    				});
							
			        		}
			        	}
						return true;
					}});
		}	
		
	}
	
	
	/** Method to set the amount entered by user in view
	 */
	private void setEnteredAmount()
	{
		TextView amountText = (TextView) findViewById (R.id.confirm_amountChosen);
		amountText.setText(String.valueOf(myAmount));		
	}
	
	/** Method to set payment methods*/
	private void setPaymentMethods()
	{
		if (myDBInteractor != null)
		{
			String previousSelection= myDBInteractor.getPrevChosenPaymentMethod(myCategoryName);
			myPaymentMethodChosen = previousSelection==null?DEFAULTPAYMENTMETHOD:previousSelection;
			LinkedHashMap<String, Integer> paymentMethods = AddAddNewToMap.getModifiedMap(myDBInteractor.getAllPaymentInfo(), PAYMENTMETHODPICID);
			if (paymentMethods != null)
			{
				
				 LinearLayout rootLayout = (LinearLayout) findViewById(R.id.confirm_paymentLinearLayout);
				 rootLayout.removeAllViews();
				 LayoutInflater inflater = (LayoutInflater) this
			    			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			    		for(Entry<String, Integer> paymentMethod: paymentMethods.entrySet())
		    			{
		    				 View view = new View(this);
		    			     view = inflater.inflate(R.layout.menu_button_home, null);
		    			     
		    			     TextView textView = (TextView) view
		    	    					.findViewById(R.id.grid_item_text);
		    	    			ImageView imageView = (ImageView) view
		    	    					.findViewById(R.id.grid_item_image);
		    	    		    
		    	    			setViewValues(rootLayout, textView, imageView, paymentMethod, view);
		    					rootLayout.addView(view);
		    					
		    			}
		     }   			
		}
	}
	
	
	/**Method to set values of the payment method views
	 * @param the text view
	 * @paaram the image view
	 * @param the map entry
	 * @param the compound view
	 */
	private void setViewValues(final LinearLayout the_parentLayout, 
			TextView the_textView, ImageView the_imageView, Entry<String, Integer> the_paymentMethod, View the_view)
	{
		if (!NullChecker.checkForNullValues(new Object[]{the_textView, the_imageView, the_paymentMethod, the_view}))
		{
			the_textView.setText(the_paymentMethod.getKey());
			the_imageView.setImageResource(the_paymentMethod.getValue());
			the_view.setClickable(true);
			the_view.setPadding(DEFAULTPADDING, DEFAULTPADDING, DEFAULTPADDING, DEFAULTPADDING);
			the_view.setFocusable(true); 
			if (myPaymentMethodChosen.equals(the_paymentMethod.getKey()))
				{
					the_view.setBackground(getResources().getDrawable(R.drawable.selectedvalue_click_state_colors));
					myPreviousView = the_view;
				}
				else
				{
					the_view.setBackground(getResources().getDrawable(R.drawable.click_state_colors));
				}
				the_view.setOnClickListener(new View.OnClickListener() {
					
					@Override
		
					public void onClick(View the_view) {
						if (the_view != null)
						{
							TextView textView = (TextView) the_view
		    					.findViewById(R.id.grid_item_text);
							String method = textView.getText().toString();
							if(ADDNEW.equals(method))       		
				        	{
				        		addNewPaymentMethod();
				        		
				        	}
				        	else
				        	{
				        		if (myPreviousView != null && !myPreviousView.equals(the_view))
				        		{
					        		myDBInteractor.savePaymentMethodChoice(myCategoryName, method);
					        		myPaymentMethodChosen = method;
					        		the_view.setBackground(getResources().getDrawable(R.drawable.selectedvalue_click_state_colors));
			                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             		        		View previous = myPreviousView;
					        		previous.setBackground(getResources().getDrawable(R.drawable.click_state_colors));
					        		myPreviousView = the_view;
				        		}
	
				        	}
						
						}
				}
			});
		}
		
		
	}
	/**Method to change the date
	 * @param the View object
	 */
	public void changeDate(View the_view)
	{
		DateTimeManager.changeDate(this, this, (Button) findViewById(R.id.confirm_dateButton));
		
	}
	
	
	/**Method to change the time
	 * @param the View object
	 */
	public void changeTime(View the_view)
	{
		DateTimeManager.changeTime(this,this, (Button) findViewById(R.id.confirm_timeButton));

	   }
	
	/**method to add a note
	 * @param the view object
	 */
	public void addNote(View the_view)
	{
		if (the_view != null)
		{
			myNewNote = true;
			DisplayMessages.displayUserInputTextBoxAndGetInput
			(this, this, DialogBoxPrompts.DIALOGNEWNOTETITLE.getPrompt(),
						DialogBoxPrompts.DIALOGNEWNOTEPROMPT.getPrompt(), 
						DialogBoxPrompts.DIALOGOKPROMPT.getPrompt(), DialogBoxPrompts.DIALOGCANCELPROMPT.getPrompt());

		}
		
	}
	
	/** Method to save the user entry - write into DB
	 * @param the_view View object
	 * @param the subcategory
	 */
	private void saveEntry(View the_view)
	{

		
		Button date = (Button) findViewById(R.id.confirm_dateButton);
		String saveDate =  nullCheck(date.getText().toString());
		
		Button time = (Button) findViewById(R.id.confirm_timeButton);
		String saveTime = nullCheck(time.getText().toString());
	
		if (myDBInteractor.addExpenseDetails
				(mySubcategoryChosen, myCategoryName, new ExpenseDetails(DateTimeManager.getDateTime
						(DateTimeManager.parseForDBDate(saveDate), saveTime), myPaymentMethodChosen, myAmount, myNote)))
		{
			Toast.makeText(getApplicationContext(), 
					RECORDSAVEDPROMPT, Toast.LENGTH_LONG).show();
			
			GoHome.goHome(this);
		}	
	
		else
		{
					DisplayMessages.displayInternalError(this, DialogBoxPrompts.NOTSAVEDDIALOGALERT.getPrompt());
		
		}
		
		

	}
	
	
	
		
		/**Method to check if the given string is null
	 * returns "default" if null, otherwise returns the string passed
	 * @param the input string
	 * @return the result string
	 */
	private String nullCheck(String the_string)
	{
		if (the_string == null)
		{
			return DEFAULTSTRING;
		}
		return the_string;
	}

		
	
	/**Method to add new subcategory*/
	private void addNewSubcategory()
	{
		myNewSubcategory = true;
		DisplayMessages.displayUserInputTextBoxAndGetInput
		(this, this, DialogBoxPrompts.DIALOGNEWSUBCATEGORYTITLE.getPrompt(),
					DialogBoxPrompts.DIALOGNEWSUBCATEGORYPROMPT.getPrompt(), 
					DialogBoxPrompts.DIALOGOKPROMPT.getPrompt(), DialogBoxPrompts.DIALOGCANCELPROMPT.getPrompt());

	}
	
	/**Method to add new payment method*/
	private void addNewPaymentMethod()
	{
		myNewPaymentMethod = true;
		DisplayMessages.displayUserInputTextBoxAndGetInput
		(this, this, DialogBoxPrompts.DIALOGNEWPAYMENTMETHODTITLE.getPrompt(),
					DialogBoxPrompts.DIALOGNEWPAYMENTMETHODPROMPT.getPrompt(), 
					DialogBoxPrompts.DIALOGOKPROMPT.getPrompt(), DialogBoxPrompts.DIALOGCANCELPROMPT.getPrompt());

	}
	
	/* (non-Javadoc)
	 * @see com.bitBusy.manageyourriches.utilityClasses.display.DisplayInputTextUsers#getTextFromDialog(java.lang.String)
	 */
	@Override
	public void getTextFromDialog(String the_name) {
		if(the_name != null && myCategoryName != null)
		{
		
			if(myNewSubcategory)
			{
					myDBInteractor.addSubcategory(myCategoryName, myCategoryImg, the_name);
				//	myDBInteractor.saveSubcategoryChoice(myCategoryName, the_name);
					myNewSubcategory = false;

					setSubcategoryGridView();
				 

			}
			else if(myNewPaymentMethod)
			{
				myDBInteractor.addPaymentMethod(the_name);
				myDBInteractor.savePaymentMethodChoice(myCategoryName, the_name);
				myNewPaymentMethod = false;
				setPaymentMethods();
			}
			else if(myNewNote)
			{
				myNote = the_name;
				myNewNote = false;

				Toast the_toast = Toast.makeText(getApplicationContext(), 
						NOTEADDEDPROMPT, Toast.LENGTH_LONG);
				the_toast.setGravity(Gravity.NO_GRAVITY, TOASTXAXIS, TOASTYAXIS);
				the_toast.show();
			
			}
		}
		
		else
		{
    	    DisplayMessages.displayUserError(this, DialogBoxPrompts.DIALOGNONAMEENTERED.getPrompt());
		}

	}
	
	
	
	

	/* (non-Javadoc)
	 * @see com.bitBusy.manageyourriches.utilityClasses.pickers.DateChangeListener#dateHasChanged()
	 */
	@Override
	public void dateHasChanged(TextView the_view) {
		if (the_view != null)
		{
			String dateSet = the_view.getText().toString();
				if (DateTimeManager.parseForDBDate(dateSet) > DateTimeManager.parseForDBDate(DateTimeManager.getCurrentDate()))
			{
				Toast.makeText(getApplicationContext(), FUTUREDATESELECTED, Toast.LENGTH_LONG).show();
			}
			
		}
		
	}

	/* (non-Javadoc)
	 * @see com.bitBusy.manageyourriches.utilityClasses.pickers.TimeChangeListener#timeHasChanged(android.widget.TextView)
	 */
	@Override
	public void timeHasChanged(TextView the_view) {
		if (the_view != null)
		{
			String time = the_view.getText().toString();
			
			Button dateButton = (Button) findViewById(R.id.confirm_dateButton);
			String dateSet = dateButton.getText().toString();
			if (dateSet.equals(DateTimeManager.getCurrentDate()) && DateTimeManager.isAfterNow(time))
			{
				Toast.makeText(getApplicationContext(), FUTURETIMESELECTED, Toast.LENGTH_LONG).show();

			}
		}
		
		
	}

	 @Override
	  public boolean onCreateOptionsMenu(Menu the_menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.actionbar_menu, the_menu);
	    return true;
	  }
	  
	  @Override
	  public boolean onOptionsItemSelected(MenuItem the_item) {
	    ActionBarChoice.choiceMade(this, the_item.getItemId());

	    return true;
	  }
	
	
	
}
